import BtnBack from "../Utility/BtnBack";
import MonoSingle from "../Utility/MonoSingle";
import NoClick from "../Utility/NoClick";
import BackPanelEvent from "./BackPanelEvent";
const { ccclass, property } = cc._decorator;
@ccclass
export default class BaseInputEvent extends cc.Component {
	@property
	btn_back: cc.Button = null;
	@property
	btn_exit: cc.Button = null;
	@property
	public btns: Array<cc.Button> = new Array<cc.Button>();
	@property
	public preBtns: Array<cc.Button> = new Array<cc.Button>();
	sceneName: string = '';
	protected onLoad(): void {
		MonoSingle.Reset();
		let self = this;
		MonoSingle.GetTvBoxInput().addBackInputAction(this.name + "backInputAction",
			function () {
				self.backInputAction();
			});
	}

	showBtnsState(): void {
		this.InitTvBoxButtonListEvent();
		this.btns.forEach(element => {
			if (element)
				cc.error("当前按钮列表::" + element.node.name + "::::" + MonoSingle.GetTvBoxInput().getPos(element.node).x + "::::" + MonoSingle.GetTvBoxInput().getPos(element.node).y)
		});
		MonoSingle.GetTvBoxInput().savePrePanelList.forEach(element => {
			if (element)
				cc.error("保存界面列表::" + element.name)
		});
		if (MonoSingle.GetTvBoxInput().curPanel)
			cc.error("当前界面::" + MonoSingle.GetTvBoxInput().curPanel.name)
		// if (this.btn_back)
		// 	cc.error("返回按钮::" + this.btn_back.node.name)
		// if (this.btn_exit)
		// 	cc.error("离开按钮::" + this.btn_exit.node.name)
		// if (MonoSingle.GetTvBoxInput().curBtn)
		// 	cc.error("当前选中按钮::" + MonoSingle.GetTvBoxInput().curBtn.node.name)
	}

	testSendBroadcast(): void {
		if (cc.sys.os == cc.sys.OS_ANDROID) {
			// 反射调用原生的隐藏方法
			jsb.reflection.callStaticMethod(
				"org/cocos2dx/javascript/AppActivity",
				"testSendBroadcast",
				"()V"
			);
		}
	}


	start() {
		this.InitTvBoxButtonListEvent();
	}
	InitTvBoxButtonListEvent(isReset: boolean = true, imgPath = "img_select_cilck_finger", isInit: boolean = true): void {
		if (this.btns != null)
			this.btns = new Array<cc.Button>();
		this.btn_back = null;
		this.btn_exit = null;
		MonoSingle.GetTvBoxButtonListEvent().clearFingerEvent();
		MonoSingle.GetTvBoxButtonListEvent().ResetBtnListEvent();
		if (!isInit) return;
		this.SetChildBtns(isReset);
		this.btns.forEach(element => {
			let _scaleX = 1; let _scaleY = 1;
			MonoSingle.GetTvBoxButtonListEvent().AddValueToBtnListEvent(element, imgPath, _scaleX, _scaleY);
		});
	}

	SetChildBtns(isReset: boolean = true): void {
		let rootChildBtns;
		let _interactable = true;
		if (MonoSingle.GetTvBoxInput().curPanel) {
			rootChildBtns = MonoSingle.GetTvBoxInput().curPanel.getComponentsInChildren(cc.Button);
		}
		else {
			rootChildBtns = MonoSingle.canvasNode.getComponentsInChildren(cc.Button);
		}
		if (MonoSingle.GetTvBoxInput().btn_guide) {
			rootChildBtns = [];
			rootChildBtns.push(MonoSingle.GetTvBoxInput().btn_guide);
			MonoSingle.GetTvBoxInput().btn_guide = null;
		}
		// else if (MonoSingle.GetTvBoxInput().moveBtns.length > 0) {
		// 	rootChildBtns = [];
		// 	MonoSingle.GetTvBoxInput().moveBtns.forEach(element => {
		// 		rootChildBtns.push(element);
		// 	});
		// 	MonoSingle.GetTvBoxInput().moveBtns = []
		// }


		rootChildBtns.forEach(element => {
			if (!element.getComponent(BtnBack) && element.name.indexOf("btn_back") == -1 && element.name.indexOf("btn_exit") == -1 && element.interactable == _interactable && this.isParentsActive(element.node)) {
				if (!element.getComponent(NoClick))
					this.btns.push(element)
			}
			if ((element.getComponent(BtnBack)||element.name.indexOf("btn_back") == 0) && this.isParentsActive(element.node))
				this.btn_back = element;
			if (element.name.indexOf("btn_exit") == 0 && this.isParentsActive(element.node))
				this.btn_exit = element;
		});
		if (this.btns.length > 0)
			MonoSingle.GetTvBoxButtonListEvent().SetSelectedGameObjectEvent(isReset);
	}
	public isParentsActive(node: cc.Node): boolean {
		if (!node.active)
			return false;
		else {
			if (node.parent) {
				if (node.parent.name != cc.director.getScene().name)
					return this.isParentsActive(node.parent);
				else
					return true;
			}
			else
				return node.active;
		}
	}

	public InitEvent(isInit: boolean = true): void {
		let _interactable = !isInit;
		if (isInit) {
			this.preBtns.length = 0;
			this.btns.forEach(element => {
				element.interactable = _interactable;
				this.preBtns.push(element)
			});
		}
		this.preBtns.forEach(element => {
			element.interactable = _interactable;
		});
		if (!isInit) {
			this.preBtns.length = 0;
		}
		this.InitTvBoxButtonListEvent();
	}

	backInputAction(): void {
		if (this.btn_back) {
			// if (MonoSingle.GetPauseEvent() && MonoSingle.GetPauseEvent().node.active == false) {
			//     this.btn_back.clickEvents.forEach(element => {
			//         cc.error("事件名字" + element.handler);
			//         element.emit([element.handler, this.btn_back]);
			//     });
			//     MonoSingle.GetPauseEvent().node.active = true;
			//     this.InitTvBoxButtonListEvent();
			//     cc.director.pause();
			// }
			// else if (MonoSingle.GetPauseEvent() && MonoSingle.GetPauseEvent().node.active == true) {
			//     cc.director.resume();
			//     this.InitTvBoxButtonListEvent();
			//     MonoSingle.GetPauseEvent().node.active = false;
			// }
			// else {
			//     let _backpanel: boolean = false;
			//     let backPanelEvents = MonoSingle.canvasNode.getComponentsInChildren(BackPanelEvent);
			//     backPanelEvents.forEach(element => {
			//         if (element.node.active == true)
			//             _backpanel = true;
			//     });
			//     if (_backpanel) {
			//         this.btn_back.clickEvents.forEach(element => {
			//             cc.error("事件名字" + element.handler);
			//             element.emit([element.handler, this.btn_back]);
			//         });
			//     }
			// }
			if (this.btn_back.clickEvents.length)
			{
				this.btn_back.clickEvents.forEach(element => {
					cc.error("事件名字" + element.handler);
					element.emit([element.handler, element.customEventData, this.btn_back]);
				});
			}
			else
			{
				cc.Component.EventHandler.emitEvents(this.btn_back.clickEvents);
				this.btn_back.node.emit('click', this.btn_back);

			}


		}
		else if (this.btn_exit != null) {
			this.SetLeaveGameOnClick();
		}
	}

	public SetLeaveGameOnClick(): void {
		
		if (MonoSingle.GetLeaveGameEvent() == null)
			return;

		if (MonoSingle.GetGuideEvent() != null && MonoSingle.GetGuideEvent().node_guide.active)
			return;
		cc.loader.loadRes("tvBoxDatas/androidBroadcast.json", cc.JsonAsset, function (error, data) {
			if (data.json.isOnceExitGame) {
				cc.game.end();
			}
			else {
				if (MonoSingle.GetGuideEvent() != null && MonoSingle.GetLeaveGameEvent().node.active == true)
					return;
				MonoSingle.GetLeaveGameEvent().node.active = true;
				MonoSingle.GetLeaveGameEvent().initEvent();
			}

		});

	}
}
